<?php

class Area {
	const TABLE = 'area';
	
	/**
	 * Whether we are in edit mode.
	 */
	protected static $editMode = false;
	
	public static function assertInstallation() {
		if (!Data::table_exists(self::TABLE))
			Data::query('CREATE TABLE IF NOT EXISTS '.self::TABLE.' (id INTEGER PRIMARY KEY AUTOINCREMENT, name, type, pos INTEGER)');
	}
	
	public static function initEditmode()
	{
		if (Url::getVar('noedit')) return;
		
		// Add style sheet
		Client::addLibrary('modules/area/edit/edit.css');
		
		// Add client side libraries
		Client::addQuibbox();
		Client::addLibrary('client/rpc/rpc.js');
		Client::addLibrary('client/jquery/jquery.js');
		Client::addLibrary('client/jqueryui/jqueryui.js');
		Client::addLibrary('http://cachefile.net/scripts/ext/2.1/adapter/ext/ext-base.js');
		Client::addLibrary('http://cachefile.net/scripts/ext/2.1/ext-all.js');
		Client::addLibrary('http://cachefile.net/scripts/ext/2.1/resources/css/ext-all.css');
		Client::addLibrary('modules/area/edit/editor.wysiwyg.js');
		Client::addLibrary('modules/area/edit/mode.js');
		Client::addLibrary('modules/article/editmode.js');
		Client::addLibrary('modules/menu/editmode.js');
		Client::addLibrary('modules/separator/editmode.js');
		
		self::$editMode = true;
		
		Event::log('modules.area', 'Edit mode activated');
	}
	
	public static function isEditmode()
	{
		return self::$editMode;
	}
	
	/**
	 * Returns all blocks for the given area ID.
	 *
	 * @param string $name
	 * @return array
	 */
	public static function get($name) {
		$area = @Data::all('SELECT * FROM '.self::TABLE.' WHERE name = '.Data::prepareValue($name).' ORDER BY pos ASC');

		if (!count($area)) {
			// TODO: This might be called a little too often for comfort, where
			// can we move the installation routine?
			self::assertInstallation();
		} else {
			foreach ($area as $key => $value) {
				$class = $value['type'];
				$lcname = strtolower($class);
				Core::inc('modules/'.$lcname.'/'.$lcname.'.class.php');
				$area[$key] = call_user_func(array($class, 'get'), $value['id']);
			}
		}
		
		return $area;
	}
	
	public static function getById($id) {
		$area = @Data::first('SELECT * FROM '.self::TABLE.' WHERE id = '.Data::prepareValue($id));
		return $area;
	}
	
	public static function register(Block $block, $areaName) {
		$area = array();
		
		Event::log('modules.area', 'Registering new block in area "'.$areaName.'"');
		
		$area['name'] = $areaName;
		$area['type'] = get_class($block);
		
		$id = Data::insert(self::TABLE, $area);
		
		return $id;
	}
	
	public static function unregister($id) {
		Data::query('DELETE FROM '.self::TABLE.' WHERE id = '.$id);
	}
	
	public static function save($data)
	{
		Data::update(self::TABLE, $data);
	}
	
	public static function show($name)
	{
		$blocks = self::get($name);
		$mode = (self::$editMode) ? '.editmode' : '';
		
		// Create a page-specific version of the id
		$localId = (strpos($name, '#') === false) ? $name : substr($name, strpos($name, '#')+1);
		
		return Design::read('modules/area/area'.$mode.'.tpl.php', array('area' => $name, 'localId' => $localId, 'blocks' => $blocks));
	}
}

abstract class Block
{
	/**
	 * ID for this block
	 */
	public $id;
	
	public function getName()
	{
		$area = Area::getById($this->id);
		return $area['name'];
	}
}

?>